package main;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author Xu,
 * @data 2020/12/8，
 * @time 13:13
 */
public class LogMining {
    private static ArrayList<ArrayList<String>> data = new ArrayList<>();
    private static ArrayList<ArrayList<String>> myData = new ArrayList<>();
    private static int count = 0;
    private static long sumTime = 0;

    public static void main(String[] args) throws ParseException {
        long start = System.currentTimeMillis();
        String path = "./src/carsLog.txt";
        readData(path);
        System.out.println("数据导入data时间：" + (System.currentTimeMillis() - start) / 1000.0 + "s");

        searchData();

        parkTime();
        System.out.println("进出车次数：" + count / 2);
        System.out.println("累计停放秒数：" + sumTime + " s");
        createTxt();
        long end = System.currentTimeMillis();
        System.out.println("runtime: " + (end - start) / 1000.0 + " s");
    }

    /**
     * 统计所有车停车的累计时间
     */
    public static void parkTime() throws ParseException {
        for (int i = 0; i < myData.size(); i++) {
            if (myData.get(i).get(3).equals("in")) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date1 = sdf.parse(myData.get(i).get(0));
                for (int j = i; j < myData.size(); j++) {
                    if (myData.get(j).get(2).equals(myData.get(i).get(2)) && "out".equals(myData.get(j).get(3))) {
                        Date date2 = sdf.parse(myData.get(j).get(0));
                        sumTime += (date2.getTime() - date1.getTime()) / 1000;
                        break;
                    }
                }
            }
        }
    }

    /**
     * 找到自己学号的停车场，并统计进出次数
     * 记录每辆车的停放时间的总和
     */
    public static void searchData() {
        for (int i = 0; i < data.size(); i++) {
            if ("201825010318".equals(data.get(i).get(1))) {
                myData.add(data.get(i));
                count++;
            }
        }

    }


    /**
     * 读取数据
     *
     * @param path
     * @return
     */
    public static void readData(String path) {
        ArrayList<String> d = null;
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(path)));
            BufferedReader br = new BufferedReader(isr);
            String str = null;
            while ((str = br.readLine()) != null) {
                d = new ArrayList<String>();
                String[] str1 = str.split(",");
                for (int i = 0; i < str1.length; i++) {
                    d.add(str1[i]);
                }
                data.add(d);
            }
            br.close();
            isr.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取文件内容出错！");
        }
    }

    /**
     * 遍历data集合,遍历花了太多时间
     */
    public static void show() {
        for (int i = 0; i < data.size(); i++) {
            System.out.println(data.get(i));
        }
    }

    /**
     * check一下
     */
    public static void show1() {
        for (int i = 0; i < myData.size(); i++) {
            System.out.println(myData.get(i));
        }
    }

    /**
     * 生成.txt文件，下次使用直接在我学号停车场的日志里面找。
     */
    public static void createTxt() {
        String path = "./src/myData.txt";
        BufferedOutputStream bufferedOutputStream = null;
        FileOutputStream outputStream = null;
        StringBuffer stringBuffer;
        try {
            outputStream = new FileOutputStream(new File(path));
            bufferedOutputStream = new BufferedOutputStream(outputStream);
            for (int i = 0; i < myData.size(); i++) {
                stringBuffer = new StringBuffer();
                stringBuffer.append(myData.get(i).toString() + "\n");
                bufferedOutputStream.write(stringBuffer.toString().getBytes("UTF-8"));
            }
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedOutputStream.close();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
